<%@page import="DAO.DAO_Manager"%>
<script src="Assets/js/jquery.tagsinput.min.js"></script>
<script src="Assets/js/ckeditor/ckeditor.js"></script>
<script src="Assets/js/ckeditor/adapters/jquery.js"></script>

<form role="form" method="post">
  <div class="form-group">
    <label>Title</label>
    <input type="text" class="form-control" name="title" placeholder="Enter title">
  </div>
  <div class="form-group">
    <label>Category</label>
    <select name="catid" class="form-control">
        <%
            for (Category item : items) {
            %>
            <option value="<%=item.getId()%>"><%=item.getName()%></option>
            <%
            }
        %>
      
    </select>    
    
  </div>
  <div class="form-group">
    <label>Content</label>
    <textarea name="content" class="form-control" id="content"></textarea>
  </div>
  <div class="form-group">
    <label>Tags</label>
    <input type="text" name="tags" value="" class="form-control" id="tag_input"  />
  </div>    

  <button type="submit" name="submit" class="btn btn-default">Submit</button>
</form>
<script>
    $( document ).ready( function() {
        $('textarea').ckeditor();
        $('#tag_input').tagsInput();
    } );

</script>
<%
            
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String tags = request.getParameter("tags");
            String scid = request.getParameter("catid");
            
            if(title == null || content == null) {
                out.print("You have to enter all of the fields");
            }else {
                String xTags = "";
                if(tags != null) xTags = tags;
                long dateline = System.currentTimeMillis() / 1000L;
                int cid = 0;
                int mid = (new DAO_Manager()).getUid(manager);
                cid = Integer.parseInt(scid);
                
                title = new String(title.getBytes("ISO8859_1"), "UTF-8");
                String slug = AccentRemover.toUrlFriendly(title);

                content = new String(content.getBytes("ISO8859_1"), "UTF-8");
                xTags = new String(xTags.getBytes("ISO8859_1"), "UTF-8");
                
                News article = new News(title, slug, (int)dateline, content, xTags, cid, mid);
                DAO_News daonews = new DAO_News();
                int n = daonews.addNews(article);
                if(n > 0) {
                    response.sendRedirect("Dashboard.jsp");
                }
                daonews.closeConnection();
            }
%>